package com.itheima.controller.pms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.api.pms.PmsSkuStockService;
import com.itheima.vo.domain.AjaxResult;
import com.itheima.vo.pojo.PmsSkuStock;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("sku")   //这个在商品列表页面,点击sku库存会自动更新还能进行模糊查询
public class PmsSkuStockController {

    @DubboReference
    private PmsSkuStockService pmsSkuStockService;


    //批量更新sku库存信息
    @PostMapping("update/{pid}")
    public AjaxResult update(@PathVariable Long pid, @RequestBody List<PmsSkuStock> pmsSkuStock){
        //遍历,依次赋id,依次修改
        for (PmsSkuStock skuStock : pmsSkuStock) {
            skuStock.setProductId(pid);
            pmsSkuStockService.updateById(skuStock);
        }
        return AjaxResult.success("更新成功");
    }


    //根据商品ID及sku编码模糊搜索sku库存
    @GetMapping("{pid}")
    public AjaxResult findList(@PathVariable Long pid ,String keyword){

        //构造模糊查询的条件
        LambdaQueryWrapper<PmsSkuStock> queryWrapper = new LambdaQueryWrapper<>();
        //商品id相等
        queryWrapper.eq(PmsSkuStock::getProductId,pid);
        //sku编码模糊查询
        queryWrapper.like(StringUtils.isNotEmpty(keyword),PmsSkuStock::getKeyword,keyword);

        List<PmsSkuStock> list = pmsSkuStockService.list(queryWrapper);

        return AjaxResult.success(list);
    }

}
